package com.github.florent37.rxretrojsoup.ohosarsenal.slice;

import com.github.florent37.retrojsoup.RetroJsoup;
import com.github.florent37.rxretrojsoup.ResourceTable;
import com.github.florent37.rxretrojsoup.ohosarsenal.OhosArsenal;
import com.github.florent37.rxretrojsoup.ohosarsenal.ListItemProvider;
import com.github.florent37.rxretrojsoup.ohosarsenal.Project;
import com.github.florent37.rxretrojsoup.rxoh.schedulers.OhosSchedulers;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.BiFunction;
import io.reactivex.schedulers.Schedulers;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;


public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private List<Project> items = new ArrayList<>() ;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        loadWithRetroJsoup();
        Observable.zip(
                Observable.just(""),
                Observable.just("&"),
                new BiFunction<String, String, String>(){
                    @Override
                    public String apply(@NonNull String s, @NonNull String s2) throws Exception {
                        return null;
                    }
                }
        );
    }

    private void loadWithRetroJsoup() {

        final OhosArsenal tutosOhosFrance = new RetroJsoup.Builder()
                .url("https://ohos-arsenal.com/")
                .build()
                .create(OhosArsenal.class);

            tutosOhosFrance.projects()
                    .toList()
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(OhosSchedulers.mainThread())
                    .subscribe(t->initListContainer(t));

    }

    private void initListContainer(List<Project> items) {
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        ListItemProvider sampleItemProvider = new ListItemProvider(items, this);
        listContainer.setItemProvider(sampleItemProvider);
    }

}
